<%-- 
    Document   : SaveFile
    Created on : Oct 26, 2014, 3:17:24 PM
    Author     : user
--%>

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="newpackage.ConnectionFactory"%>
<%@page import="newpackage.CountWord"%>
<%@page import="newpackage.SplitFileExample"%>
<%@page import="newpackage.SendEmailKey"%>
<%@page import="java.io.FileWriter"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body background="image.jpg">
        <h1> ENSURING PRIVACY AND DATA FRESHNESS OF SHARED DATA IN CLOUD </h1>
        <%
        String UPLOAD_DIRECTORY="sub";
        StringBuffer content = new StringBuffer(request.getParameter("file_content"));
    
        String file_name=request.getParameter("file_name");
        
        String file_name_only=file_name.substring(file_name.lastIndexOf('/')+1);
        String output_folder=file_name.replace("main","sub");
        
        
        
       
        File file = new File(output_folder);
        
      // creates the file
          file.createNewFile();
      // creates a FileWriter Object
         FileWriter writer = new FileWriter(file); 
      // Writes the content to the file
         writer.write(content.toString()); 
         writer.flush();
         writer.close();
         
         SplitFileExample.split(output_folder);
        
        if(CountWord.getCount(file_name)!=CountWord.getCount(output_folder))
                       {
            out.print("The file has been modified");
            Connection con=ConnectionFactory.getConnection();
            Statement s=con.createStatement();
            ResultSet rs=s.executeQuery("Select * from user_details");
            String toMail="";
            if(rs.next())
            {
                toMail=rs.getString(3);
            }
SendEmailKey.sendMail(toMail,file_name_only+" has been modified by this user  :"+(String)session.getAttribute("user_name"));
        }
         else
                         {
             out.print("The file has been Saved");
         }
        %>
        <a href="ViewFile.jsp">Back</a>
    </body>
</html>
